package org.lee.handle.impl;

import lombok.extern.slf4j.Slf4j;
import org.lee.handle.HandlerInterceptor;
import org.lee.handle.SsoDecorator;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * LoginSsoDecorator
 * 登录sso装饰类
 *
 * @author dralee
 * @date 2021-11-22 16:40:12
 **/
@Slf4j
public class LoginSsoDecorator extends SsoDecorator {

    private static Map<String, String> authMap = new ConcurrentHashMap<>();

    static {
        authMap.put("lee", "queryUserInfo");
        authMap.put("mike", "queryUserInfo");
        authMap.put("jim", "queryUserInfo");
    }

    public LoginSsoDecorator(HandlerInterceptor handlerInterceptor) {
        super(handlerInterceptor);
    }

    @Override
    public boolean preHandle(String request, String response, Object handler) {
        boolean res = super.preHandle(request, response, handler);
        if (!res) {
            return false;
        }
        if (request.length() < 12) {
            log.warn("request invalid: {}", request);
            return false;
        }
        String userId = request.substring(10);
        String authMethod = authMap.get(userId);
        log.info("模拟单点登录访问拦截校验：{} {}", userId, authMethod);
        return "queryUserInfo".equals(authMethod);
    }
}
